#弹幕解析转UDP
from bilibili_api import live, sync
import socket
import struct
import datetime
#client 发送端
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
PORT = 3333
a=[0xff,0x03,0xff]

room = live.LiveDanmaku(22658118)



@room.on('DANMU_MSG')
async def on_danmaku(event):
    # 收到弹幕
    #print(event)
    ll=event.get('data').get('info')
    msg=ll[1]
    tmp1=ll[2]
    uname=tmp1[1]
    print(uname)
    print(msg)
    if  msg =="开灯":
        if datetime.datetime.now().hour>=7 and datetime.datetime.now().hour<12 :
            a[0]=0xff
            a[1]=0x03
            a[2]=0xff
        elif datetime.datetime.now().hour>=18 and datetime.datetime.now().hour<23 :
            a[0]=0xff
            a[1]=0x03
            a[2]=0x50
        date=struct.pack("%dB"%(len(a)),*a)
        server_address = ("192.168.31.101", PORT)  # 接收方 服务器的ip地址和端口号
        client_socket.sendto(date, server_address) #将msg内容发送给指定接收方
    elif  msg =="关灯":
        if datetime.datetime.now().hour>=7 and datetime.datetime.now().hour<12 :
            a[0]=0xff
            a[1]=0x03
            a[2]=0x40
        elif datetime.datetime.now().hour>=18 and datetime.datetime.now().hour<23 :
            a[0]=0xff
            a[1]=0x03
            a[2]=0x40
        date=struct.pack("%dB"%(len(a)),*a)
        server_address = ("192.168.31.101", PORT)  # 接收方 服务器的ip地址和端口号
        client_socket.sendto(date, server_address) #将msg内容发送给指定接收方
    elif  msg =="touwei":
        a[0]=0xff
        a[1]=0x01
        a[2]=0x0f
        date=struct.pack("%dB"%(len(a)),*a)
        server_address = ("192.168.31.126", PORT)  # 接收方 服务器的ip地址和端口号
        client_socket.sendto(date, server_address) #将msg内容发送给指定接收方


@room.on('SEND_GIFT')
async def on_gift(event):
    # 收到礼物
    print(event)


sync(room.connect())
